package com.gjy.concurrency.util;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.LongAdder;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-02-17 13:56:36
 */
public class UtilTest {

    @Test
    public void test4() {
        CyclicBarrier barrier = new CyclicBarrier(2);

    }

    @Test
    public void test3() throws ExecutionException, InterruptedException {
        ExecutorService pool = Executors.newFixedThreadPool(5);
        LongAdder adder = new LongAdder();
        List<Future<?>> list = new ArrayList<>();

        for (int i = 0; i < 1000; i++) {
            Future<?> submit = pool.submit(() -> {
                System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
                adder.increment();
            });
            list.add(submit);
        }

        for (Future<?> future : list) future.get();

        System.out.println("adder = " + adder);
    }

    @Test
    public void test2() throws InterruptedException {
        CountDownLatch cdl = new CountDownLatch(2);
        new Thread(() -> {
            System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
            cdl.countDown();
        }).start();

        new Thread(() -> {
            System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
            cdl.countDown();
        }).start();

        cdl.await();

        System.out.println("HELLO WORLD");
    }

    @Test
    public void test1() {
        FutureTask<String> ft = new FutureTask<>(() -> {
            return "";
        });
    }
}
